package com.xyht.medicine_management_system.modules.order.controller;


import com.xyht.medicine_management_system.auth.util.JwtTokenUtil;
import com.xyht.medicine_management_system.common.annotation.RequestPass;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.medicine_management_system.modules.order.entity.req.CheckoutReq;
import com.xyht.medicine_management_system.modules.order.entity.req.MmsOrderReq;
import com.xyht.medicine_management_system.modules.order.entity.req.SubmitOrderReq;
import com.xyht.medicine_management_system.modules.order.service.MmsOrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-12-14
 */
@RestController
@SecretBody
@RequestMapping("/order")
public class MmsOrderController {
    @Resource
    private MmsOrderService mmsOrderService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiOperation("计算金钱")
    @GetMapping("/calculate")
    public ResponseResult calculateMoney(@RequestBody CheckoutReq checkoutReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return mmsOrderService.calculateMoney(checkoutReq, userId);
    }

    @ApiOperation("结算")
    @RequestPass
    @PostMapping("/checkout")
    public ResponseResult checkout(@RequestBody CheckoutReq checkoutReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return mmsOrderService.checkout(checkoutReq, userId);
    }

    @ApiOperation("提交订单")
    @CrossOrigin // 跨域
    @PostMapping("/submit")
    public ResponseResult submitOrder(@RequestBody SubmitOrderReq orderReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return mmsOrderService.submitOrder(orderReq, userId);
    }

    @ApiOperation("去支付")
    @CrossOrigin // 跨域
    @PostMapping("/payment")
    public ResponseResult payment(@RequestBody MmsOrderReq orderReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return mmsOrderService.payment(orderReq, userId);
    }

    @ApiOperation("取消订单")
    @PostMapping("/cancel")
    public ResponseResult cancelOrder(@RequestBody MmsOrderReq orderReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return mmsOrderService.cancelOrder(orderReq, userId);
    }

    @ApiOperation("我的订单")
    @GetMapping("/userOrder")
    public ResponseResult userOrder(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                    @RequestParam(value = "searchStr", required = false) String searchStr,
                                    @RequestParam(value = "orderStatus", required = false) Integer orderStatus,
                                    HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return mmsOrderService.userOrder(pageNum, pageSize, searchStr, orderStatus, userId);
    }

    @ApiOperation("订单详情")
    @GetMapping("/detail")
    public ResponseResult orderDetail(@RequestParam("orderId") String orderId, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return mmsOrderService.orderDetail(orderId, userId);
    }

    @ApiOperation("确认发货")
    @PostMapping("/confirmDeliver")
    public ResponseResult confirmDeliver(@RequestParam("orderId") String[] orderId, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return mmsOrderService.confirmDeliver(orderId, userId);
    }

    @ApiOperation("确认收货")
    @PostMapping("/takeDelivery")
    public ResponseResult takeDelivery(@RequestParam("orderId") String orderId, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return mmsOrderService.takeDelivery(orderId, userId);
    }

    @ApiOperation("获取所有订单")
    @GetMapping("/allOrder")
    public ResponseResult getAllOrder(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                      @RequestParam(value = "searchStr", required = false) String searchStr,
                                      @RequestParam(value = "orderStatus", required = false) Integer orderStatus,
                                      @RequestParam(value = "isClose", required = false) Integer isClose) {
        return mmsOrderService.getAllOrder(pageNum, pageSize, searchStr, orderStatus, isClose);
    }

}

